/*
 * @Author: zhousg
 * @Date: 2020-09-30 17:07:44
 * @Last Modified by: zhousg
 * @Last Modified time: 2020-10-30 16:30:23
 */
import http from '@/utils/request'
import { getUser } from '@/utils/local'
const KEY = 'erabbit-client-pc-cart'
/**
  * 加入购物车
  * @param {Object} goods - 商品信息
  * @param {Number} goods.id - 商品ID
  * @param {Number} goods.name - 商品名称
  * @param {Array<object>} goods.attrs - 商品规格
  * @param {Number} goods.num - 商品件数
  */
export const addCart = (goods) => {
  // 示例： goods ===> {id,name,num,attrs:[{id:'',name:'',groupId:'',groupName:''},...]}
  return new Promise((resolve, reject) => {
    const { token } = getUser()
    if (token) {
      // TODO
    } else {
      const cartList = JSON.parse(window.localStorage.getItem(KEY) || '[]')
      const sameGoods = cartList.find(item => item.skuId === goods.skuId)
      if (sameGoods) {
        sameGoods.count += goods.count
      } else {
        cartList.unshift({ ...goods, selected: false })
      }
      window.localStorage.setItem(KEY, JSON.stringify(cartList))
      resolve()
    }
  })
}
/**
 * 删除购物车-单个商品
 * @param {Object} delItem - 购物车数组中的单个对象
 */
export const delCart = (delItem) => {
  return new Promise((resolve, reject) => {
    const { token } = getUser()
    if (token) {
      // TODO
    } else {
      const cartList = JSON.parse(window.localStorage.getItem(KEY) || '[]')
      const index = cartList.findIndex(item => item.skuId === delItem.skuId)
      cartList.splice(index, 1)
      window.localStorage.setItem(KEY, JSON.stringify(cartList))
      resolve()
    }
  })
}
/**
 * 删除购物车-单个商品
 * @param {Attay<object>} arrGoods - 购物车数组
 */
export const batchDelCart = (arrGoods) => {
  return new Promise((resolve, reject) => {
    const { token } = getUser()
    if (token) {
      // TODO
    } else {
      const cartList = JSON.parse(window.localStorage.getItem(KEY) || '[]')
      arrGoods.forEach(delItem => {
        const index = cartList.findIndex(item => item.skuId === delItem.skuId)
        cartList.splice(index, 1)
      })
      window.localStorage.setItem(KEY, JSON.stringify(cartList))
      resolve()
    }
  })
}
/**
 * 获取购物车信息
 */
export const getCart = () => {
  return new Promise((resolve, reject) => {
    const { token } = getUser()
    if (token) {
      http.get('member/cart').then(res => {
        resolve(res.data.result)
      }).catch(e => {
        reject(e)
      })
    } else {
      const cartList = JSON.parse(window.localStorage.getItem(KEY) || '[]')
      const promiseAll = cartList.map(({ skuId }) => http.get('goods/info', { skuId }))
      Promise.all(promiseAll).then(results => {
        resolve(cartList.map((item, i) => ({ ...item, ...results[i].data })))
      })
    }
  })
}

/**
 * 更新本地购物车
 * @param {Array<object>} cartList - 购物车数组
 */
export const updateLoclCart = (cartList) => {
  const myCart = cartList.map(({ id, skuId, attrsText, picture, name, price, count, selected }) => {
    return { id, skuId, attrsText, picture, name, price, count, selected }
  })
  window.localStorage.setItem(KEY, JSON.stringify(myCart))
}

/**
 * 合并购物车信息
 */
export const mergeCart = () => {}
